package koction.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import koction.model.admin.AskSellerVO;
import koction.model.consumer.CartVO;
import koction.model.consumer.ConsumerService;
import koction.model.consumer.ConsumerVO;
import koction.model.consumer.GradeVO;
import koction.model.product.ProductVO;
import koction.model.purchaseListBoard.ListVO;
import koction.model.seller.SellerVO;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ConsumerController {
	@Resource(name="consumerServiceImpl")
	private ConsumerService consumerService;
	
	@RequestMapping("/consumer.do")
	public String asd(){
		return "index";
	}	
	@RequestMapping("/consumer_login.do")
	public String loginConsumerPage(){
		return "loginConsumer.consumer";
	}
	@RequestMapping("/consumer_register.do")
	public String registerConsumerPage(){
		return "registerConsumer.consumer";
	}
	@RequestMapping("/consumer_update.do")
	public String updateConsumerPage(){
		return "updateConsumer.consumer";
	}
	
	@RequestMapping("/consumer_info.do")
	public ModelAndView consumerInfoPage(ConsumerVO cvo){
		cvo=consumerService.findConsumerById(cvo.getId());
		System.out.println(cvo);
		return new ModelAndView("consumerInfo.consumer","cvo",cvo);
	}
	@RequestMapping("/loginconsumer.do")
	public ModelAndView loginConsumer(HttpServletRequest request ,HttpSession session, ConsumerVO cvo){
		ConsumerVO cvo1=consumerService.loginConsumer(cvo);	
		String path="";
		if(cvo1==null){
			path="loginConsumerFail.consumer";
		} else {
			SellerVO svo=consumerService.findSellerByConsumerId(cvo1.getId());
			session.setAttribute("cvo", cvo1);
			HttpSession session2=request.getSession(false);
			session2.setAttribute("svo",svo);
			path="home";
		}
		return new ModelAndView(path);
	}
	
	@RequestMapping("/logoutconsumer.do")
	public ModelAndView logoutConsumer(HttpSession session){
		session.invalidate();
		return new ModelAndView("home");
	}
	
	@RequestMapping("/idcheckconsumer.do")
	@ResponseBody
	public Map consumerIdCheck(String id){
		ConsumerVO cvo=consumerService.consumerIdCheck(id);
		Map map=new HashMap();
		map.put("cvo", cvo);
		return map;
	}
	
	@RequestMapping("/registerconsumer.do")
	public ModelAndView registerConsumer(ConsumerVO cvo){
		int result=consumerService.registerConsumer(cvo);
		return new ModelAndView("registerConsumerOk.consumer");
	}
	
	@RequestMapping("/updateconsumer.do")
	public ModelAndView updateConsumer(HttpSession session, ConsumerVO cvo){
		consumerService.updateConsumer(cvo);
		session.setAttribute("cvo", cvo);
		return new ModelAndView("updateConsumerOk.consumer");
	}
	
	@RequestMapping("/deleteconsumer.do")
	public ModelAndView deleteConsumer(HttpSession session, ConsumerVO cvo){
		ConsumerVO cvo1=(ConsumerVO) session.getAttribute("cvo");
		consumerService.deleteConsumer(cvo1);
		session.invalidate();
		return new ModelAndView("deleteconsumerOK.consumer");
	}
	
	/**
	 * 장바구니에 담기
	 * @param pvo
	 * @param session
	 */
	@RequestMapping("/addCartList.do")
	@ResponseBody
	public void addCartList(ProductVO pvo, int amount, HttpSession session){
		ConsumerVO vo=(ConsumerVO) session.getAttribute("cvo");
		ArrayList<CartVO> list=vo.getList();
		boolean f=true;
		//동일한 상품이 있는지 확인
		for(int i=0; i<list.size(); i++){ 
			if(list.get(i).getProductId()==pvo.getProductId()){
				list.get(i).setAmount(list.get(i).getAmount()+amount);
				f=false;
			}
		}if(f){
			System.out.println(pvo);
			list.add(new CartVO(pvo.getProductId(), amount,pvo.getProductPrice(), pvo.getProductName()));
		}
	}
	
	/**
	 * 장바구니 list불러오기
	 * @param session
	 * @return
	 */
	@RequestMapping("/getCartList.do")
	public ModelAndView getCartList(HttpSession session){
		ConsumerVO vo=(ConsumerVO)session.getAttribute("cvo");
		ArrayList<CartVO> list=vo.getList();
		return new ModelAndView("cartList.consumer","list", list);
	}
	/**
	 * Point구매페이지로 이동
	 * @return
	 */
	@RequestMapping("/purchasePointView.do")
	public String purchasePointView(){
		System.out.println("포인트 구매페이지로~");
		return "purchasePoint.consumer";
	}
	
	/**
	 * Point구매
	 * @return
	 */
	@RequestMapping("/puchasePoint.do")
	@Transactional
	public String puchasePoint(ConsumerVO cvo, HttpSession session){
		consumerService.puchasePoint(cvo);
		ConsumerVO vo=consumerService.findConsumerById(cvo.getId());
		GradeVO gvo=consumerService.myGrade(vo);
		
		Map map=new HashMap();
		if(gvo!=null){
		map.put("consumerId", vo.getId());
		map.put("grade", gvo.getGrade());
		consumerService.updateGrade(map);
		}
		ConsumerVO vo1=consumerService.findConsumerById(cvo.getId());
		ConsumerVO vo2=(ConsumerVO)session.getAttribute("cvo");
		vo1.setList(vo2.getList());
		session.setAttribute("cvo", vo1);
		return "purchasePointResult.consumer";
	}
	/**
	 * 상품구매
	 * @param pvo
	 * @param amount
	 * @param session
	 * @return
	 */
	@RequestMapping("/buyProductView.do")
	public ModelAndView buyProductView(ProductVO pvo, int amount, HttpSession session){
		System.out.println(pvo+" "+amount);
		int totalPrice=pvo.getProductPrice()*amount;
		Map map=new HashMap();
		map.put("pvo", pvo);
		map.put("amount", amount);
		map.put("totalPrice",totalPrice);
		return new ModelAndView("buyProductView.consumer","map",map);
	}
	
	@RequestMapping("/buyProduct.do")
	public ModelAndView buyProduct(ProductVO pvo, int amount, ConsumerVO cvo,HttpSession session){
		consumerService.buyProduct(pvo, amount, cvo, cvo.getPoint());
		int myPoint=consumerService.findConsumerById(cvo.getId()).getPoint();
		ConsumerVO vo=(ConsumerVO) session.getAttribute("cvo");
		vo.setPoint(myPoint);
		session.setAttribute("cvo", vo);
		return new ModelAndView("buyProductOk.consumer");
	}
	
	/**
	 * 구매내역 페이지로
	 * @param session
	 * @return
	 */
	@RequestMapping("/getPurchaseList.do")
	public ModelAndView getPurchaseList(String pageNo, HttpSession session){
		ConsumerVO cvo=(ConsumerVO) session.getAttribute("cvo");
		ListVO lvo=consumerService.getPurchaseList(cvo.getId(), pageNo);
		return new ModelAndView("purchaseList.consumer","lvo",lvo);
	}
	/**
	 * 구매내역 삭제
	 */
	@RequestMapping("/deletePurchaseList.do")
	@ResponseBody
	public ListVO deletePurchaseList(String[] purchaseNo, String id, String pageNo){
		consumerService.deletePurchaseList(purchaseNo);
		ListVO lvo=consumerService.getPurchaseList(id, pageNo);
		return lvo;
	}
	/**
	 * 장바구니에서 상품구매
	 */
	@RequestMapping("/buyProductFromCart.do")
	public ModelAndView buyProductFromCart(int[] productId,int[] amount, ConsumerVO cvo, int[] mPoint, HttpSession session){
		System.out.println(mPoint[0]);
		for(int i=0; i<productId.length; i++){
			//상품정보 받아오기
			ProductVO pvo=consumerService.findProductById(productId[i]);
			//상품구매
			consumerService.buyProduct(pvo, amount[i], cvo, mPoint[i]);
			//장바구니 상품 삭제
			this.deletProductFormCart(productId, session);
		}
		int myPoint=consumerService.findConsumerById(cvo.getId()).getPoint();
		ConsumerVO vo=(ConsumerVO) session.getAttribute("cvo");
		vo.setPoint(myPoint);
		session.setAttribute("cvo", vo);
		return new ModelAndView("buyProductOk.consumer");
	}
	/**
	 * 장바구니 상품 삭제
	 */
	@RequestMapping("/deletProductFormCart.do")
	@ResponseBody
	public ArrayList<CartVO> deletProductFormCart(int[] productId, HttpSession session){
		ConsumerVO vo=(ConsumerVO)session.getAttribute("cvo");
		ArrayList<CartVO> list=vo.getList();
		//장바구니에서 삭제할 상품아이디를 체크하고 삭제한다.
		for(int i=0; i<productId.length; i++){
			for(int j=0; j<list.size(); j++){
				if(list.get(j).getProductId()==productId[i]){
					list.remove(j);
				}
			}
		}
		return list;
	}
	/**
	 * 판매자 신청하는 페이지 이동하는 메서드
	 */
	@RequestMapping("/askSellerView.do")
	public String askSellerView(){
		return "askSellerView.consumer";
	}
	/**
	 * 판매자 신청하는 메서드
	 */
	@RequestMapping("/askSeller.do")
	public String askSeller(AskSellerVO asvo,HttpSession session){
		ConsumerVO vo=(ConsumerVO)session.getAttribute("cvo");
		asvo.setId(vo.getId());
		asvo.setTotalPoint(vo.getTotalPoint());
		consumerService.askSeller(asvo);
		return "askSeller.consumer";
	}
}
